package com.campus.marketplace.service.impl;

import com.campus.marketplace.entity.Report;
import com.campus.marketplace.entity.OperationLog;
import com.campus.marketplace.repository.ReportRepository;
import com.campus.marketplace.repository.OperationLogRepository;
import com.campus.marketplace.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportRepository reportRepository;

    @Autowired
    private OperationLogRepository operationLogRepository;

    @Override
    @Transactional
    public Report submitReport(Report report) {
        report.setStatus("PENDING");
        Report savedReport = reportRepository.save(report);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(report.getReporter().getId());
        log.setOperationType("SUBMIT_REPORT");
        log.setTargetType("REPORT");
        log.setTargetId(savedReport.getId());
        log.setOperationContent("提交举报: " + report.getContent());
        operationLogRepository.save(log);

        return savedReport;
    }

    @Override
    public List<Report> getUserReports(Long userId) {
        return reportRepository.findByReporterIdOrderByCreatedAtDesc(userId);
    }

    @Override
    public List<Report> getTargetReports(String targetType, Long targetId) {
        return reportRepository.findByTargetTypeAndTargetIdOrderByCreatedAtDesc(targetType, targetId);
    }

    @Override
    public List<Report> getPendingReports() {
        return reportRepository.findByStatusOrderByCreatedAtDesc("PENDING");
    }

    @Override
    public Report getReportById(Long reportId) {
        return reportRepository.findById(reportId)
                .orElseThrow(() -> new RuntimeException("Report not found"));
    }

    @Override
    @Transactional
    public void processReport(Long reportId, String status, String processResult) {
        Report report = reportRepository.findById(reportId)
                .orElseThrow(() -> new RuntimeException("Report not found"));

        report.setStatus(status);
        report.setContent(report.getContent() + "\n处理结果: " + processResult);
        reportRepository.save(report);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(report.getReporter().getId());
        log.setOperationType("PROCESS_REPORT");
        log.setTargetType("REPORT");
        log.setTargetId(reportId);
        log.setOperationContent("处理举报: " + processResult);
        operationLogRepository.save(log);
    }

    @Override
    @Transactional
    public void updateReportStatus(Long reportId, String status) {
        Report report = reportRepository.findById(reportId)
                .orElseThrow(() -> new RuntimeException("Report not found"));

        report.setStatus(status);
        reportRepository.save(report);

        // 记录操作日志
        OperationLog log = new OperationLog();
        log.setUserId(report.getReporter().getId());
        log.setOperationType("UPDATE_REPORT_STATUS");
        log.setTargetType("REPORT");
        log.setTargetId(reportId);
        log.setOperationContent("更新举报状态: " + status);
        operationLogRepository.save(log);
    }

    @Override
    public Long getReportCount(String targetType, Long targetId) {
        return reportRepository.countByTarget(targetType, targetId);
    }

    @Override
    public List<Report> getReportsByTypeAndStatus(String reportType, String status) {
        return reportRepository.findByStatusAndReportType(status, reportType);
    }
}